package org.jeecg.modules.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.TreeSelectModelVo;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.entity.DiyProfessorInfo;
import org.jeecg.modules.mapper.DiyProfessorInfoMapper;
import org.jeecg.modules.service.IDiyProfessorInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Date 2021/4/25 16:03
 * @Author WangPeng
 * @Version 1.0
 */
@Api(tags = "项目所需专家抽取状态")
@RestController
@RequestMapping("/own/category")
public class OwnCategoryController {

    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private DiyProfessorInfoMapper diyProfessorInfoMapper;
    /**
     * 抽取时查找专家类别别时带上人数
     * @param pid
     * @param pcode
     * @param condition
     * @return
     */
    @RequestMapping(value = "/loadTreeData", method = RequestMethod.GET)
    public Result<List<TreeSelectModelVo>> loadDict(@RequestParam(name="pid",required = false) String pid, @RequestParam(name="pcode",required = false) String pcode, @RequestParam(name="condition",required = false) String condition) {
        Result<List<TreeSelectModelVo>> result = new Result<List<TreeSelectModelVo>>();
        //pid如果传值了 就忽略pcode的作用
        if(oConvertUtils.isEmpty(pid)){
            if(oConvertUtils.isEmpty(pcode)){
                result.setSuccess(false);
                result.setMessage("加载分类字典树参数有误.[null]!");
                return result;
            }else{
                if("0".equals(pcode)){
                    pid = "0";
                }else{
                    pid = sysBaseAPI.queryIdByCode(pcode);
                }
                if(oConvertUtils.isEmpty(pid)){
                    result.setSuccess(false);
                    result.setMessage("加载分类字典树参数有误.[code]!");
                    return result;
                }
            }
        }
        Map<String, String> query = null;
        if(oConvertUtils.isNotEmpty(condition)) {
            query = JSON.parseObject(condition, Map.class);
        }
        List<TreeSelectModelVo> collect = sysBaseAPI.queryListByPid(pid,query);
        //查询出所有在职，没请假的专家
        QueryWrapper<DiyProfessorInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_leave",0).eq("is_work",0).eq("del_flag",0);
        List<DiyProfessorInfo> diyProfessorInfolist = diyProfessorInfoMapper.selectList(queryWrapper);
        //查出对应人数，拼接到名字后面
        List<TreeSelectModelVo> ls = collect.stream().map(item -> {
            HashMap<String, Integer> flag = new HashMap<>();
            int sums = 0;
            TreeSelectModelVo treeSelectModel = new TreeSelectModelVo();
            String key = item.getKey();
            if (key != null) {
                if (sysBaseAPI.griPidById(key).equals("1261524044952387585")) {
                    List<String> strings = sysBaseAPI.gitIdByPid(key);
                    for (String s:strings) {
                        for (DiyProfessorInfo list : diyProfessorInfolist) {
//                            如果一个专家在一个大类下有多个子类别，我们算一人就好
                            int Pcount=0;
                            String professorDeclareCategory = list.getProfessorDeclareCategory();
                            String professorName = list.getProfessorName();
                            String[] split =  professorDeclareCategory.split(",");
                            if(list.getIsChangeInfo()!=0 ||list.getIsLeave()!=0 || list.getIsWork()!=0 ){
                                 // 说明专家的状态如果是修改信息状态等不统计该专家
                                break;
                            }
                            if (flag.get(professorName)!=null){
                                break;
                            }
                            for (String value : split) {
                                for (String string : strings) {
                                    if(value.equals(string)){
                                        Pcount++;
                                    }
                                }
                            }
                            if(Pcount>0){
                                sums+=1;
                                flag.put(list.getProfessorName(),Pcount);
                            }
                        }
                    }
                } else {
                    for (DiyProfessorInfo list : diyProfessorInfolist) {
                        if(list.getIsChangeInfo()!=0 ||list.getIsLeave()!=0 || list.getIsWork()!=0 ){
                            // 说明专家的状态如果是修改信息状态等不统计该专家
                            break;
                        }
                        String[] split = list.getProfessorDeclareCategory().split(",");
                        for (int i = 0; i < split.length; i++) {
                            if (split[i].equals(key)) {
                                sums+=1;
                            }
                        }
                    }
                }

//                System.out.println("key=============>"+sums+"r人");
                item.setTitle(item.getTitle() + "  " + sums + "人");
            }
            BeanUtils.copyProperties(item,treeSelectModel);
            return treeSelectModel;
        }).collect(Collectors.toList());

        result.setSuccess(true);
        result.setResult(ls);
        return result;
    }
}
